<?php
class tools extends Admin_Controller  {
	public function __construct() {
        parent::__construct();
       	$this->load->library('simple_html_dom');
    }

    public function index($value='') {
		$web[] = "/lam-sao-de-co-mon-thit-ba-chi-nuong-rieng-me-p214a17758.html";
		$web[] = "/lam-sao-de-tiet-kiem-thoi-gian-khi-nau-nuong-p214a17602.html";
		$web[] = "/canh-ga-nuong-sa-kieu-bbq-p214a73609.html";
		$web[] = "/canh-ga-hun-khoi-thom-nuc-cho-bua-com-ngay-mua-p214a72672.html";
		$web[] = "/lam-mon-thit-lon-nuong-mat-ong-dam-da-cho-bua-toi-p214a76367.html";
		$web[] = "/ngo-nuong-mat-ong-thom-lung-cho-buoi-toi-mat-troi-p214a76275.html";
		$web[] = "/vao-bep-lam-2-mon-chan-ga-nuong-ngon-tuyet-p214a55030.html";
		$web[] = "/lam-mon-ga-nuong-xien-dai-khach-p214a75418.html";
		$web[] = "/trung-nuong-ca-chua-doc-dao-p214a75039.html";
		$web[] = "/ngao-nuong-bo-thom-nuc-mui-p214a74948.html";
		$web[] = "/bo-cuon-nam-nuong-giay-bac-p214a74629.html";
		$web[] = "/lam-ca-chinh-nuong-kieu-nhat-p214a74429.html";
		$web[] = "/tom-nuong-xot-ot-tabasco-p214a74391.html";
		$web[] = "/ga-nuong-cay-mon-ngon-dai-khach-p214a74216.html";
		$web[] = "/thit-lon-nuong-kieu-bbq-ngon-ngay-ngat-p214a74096.html";
		$web[] = "/tom-nuong-sa-mon-ngon-dai-chong-p214a73906.html";
		$web[] = "/thit-bo-nuong-sa-thom-lung-hap-dan-p214a73601.html";
		$web[] = "/khoai-tay-nuong-bo-toi-thom-lung-hap-dan-p214a73520.html";
		$web[] = "/chuoi-boc-nep-nuong-an-vat-ngon-ngay-mat-troi-p214a73111.html";
		$web[] = "/khoai-lang-nuong-day-loi-cuon-p214a73085.html";
		$web[] = "/chao-tom-nuong-cho-ngay-mat-troi-p214a72970.html";
		$web[] = "/ca-hoi-nuong-xot-tieu-va-rau-mui-p214a72950.html";
		$web[] = "/tom-nuong-cay-thom-hap-dan-p214a72902.html";
		$web[] = "/ca-nh-ga-va-rau-cu-nuo-ng-p214a72747.html";

		$url = "http://www.lamsao.com";

		foreach ($web as $key => $value) {
			$string = $url . $value;
			$this->exportData($string);
			echo "<br/>" . $string;
		}

		/*$web = "http://www.lamsao.com/muop-dang-nhoi-dau-phu-la-mieng-p214a60352.html";
		$this->exportData($web);*/
    }

    public function exportData($web) {
    	error_reporting(E_ALL & ~E_NOTICE);
    	try {
    	$html = file_get_html($web);

    	$data['name'] = $this->decode($html->find('.newsContainer .newsTitle', 0)->plaintext);
    	$data['image'] = $this->decode($html->find('.newsImage img', 0)->src);
    	$data['introtext'] = $this->decode($html->find('.newsDescription', 0)->plaintext);
    	$data['is_hot'] = 0;
    	$data['status'] = 2;
    	$data['fulltext'] = $this->decode($html->find('.newsTipsAndWarning table.mainContent', 0)->plaintext);
    	$data['prepTime'] = 10;
    	//$data['created'] = 0;
    	$data['cookTime'] = 15;
    	$data['recipeYield'] = 4;
    	
    	$data['is_spec'] = 0;
    	$data['rank'] = 4;
    	$data['view'] = 110;
    	$data['category_id'][] = 145;
    	$data['category_id'][] = 97;
    	$data['category_id'][] = 99;
    	$data['category_id'][] = 109;
    	$data['category_id'][] = 101;
    	$data['category_id'][] = 106;
    	$data['source'] = $web;
    	//var_dump($data); exit();
    	$config['upload_path'] = './images/content/step/';
    	$filename = $this->getname();
    	$file = ".jpg";

    	$data['tags'] = $html->find("meta[name=KEYWORDS]", 0)->getAttribute('content');;

    	foreach($html->find('.detailContent div') as $p) {
		    $string = $this->decode($p->plaintext);//trim($p->find('p', 0)->outertext);

		    if ($string == '1') {
		    	break;
		    }

		    $data['stock'][] = $string;
		}

		if (!isset($data['stock'])) {
			foreach($html->find('.detailContent p') as $p) {
				//$string = $p->children(1)->tag;
			    $string = $this->decode($p->plaintext);

			    //var_dump($string);

			    if ($string == '1') {
			    	break;
			    }

			    $data['stock'][] = $string;
			}
		}
		

		if (!isset($data['stock'])) {
			$data['stock'][] = $web;
		}

		
		$i = 1;
		foreach($html->find('ul.how li.jsArticleStep') as $p) {
		    $data['steps'][$i]['comment'] = $this->decode($p->find('.stepContent', 0)->plaintext);
		    
		    $url = trim($p->find('img', 0)->src);
		    if ($url)
		    {
		    	
		    	$full_path = $config['upload_path'] . $this->getname() . $file;
		    	file_put_contents($full_path, file_get_contents($url));
			    
				parent::watermark($full_path);
	            $data['steps'][$i]['image'] = $full_path;
            }
		    $i++;
		}

		$url = $data['image'];

		file_put_contents($config['upload_path'] . $filename . $file, file_get_contents($url));

		$data['image1'] = $config['upload_path'] . $filename . $file;

		$this->process_data($data);
		//var_dump($data);*/

		echo "đã hoàn thành";
		//var_dump($data);
		//
		} catch (Exception $e) {
		    echo 'Caught exception: ',  $e->getMessage(), "\n";
		}
    }

    function getname()
    {
    	$text = random_string('alnum', 4);
    	$time = time();

    	return $time . '_' . $text;
    }

    function decode($string ='')
    {
    	$string =  trim($string);
    	return html_entity_decode($string, ENT_QUOTES, "utf-8");
    }

	function process_data($data='') {
		$this->load->model('dish/dish_model');
	    
        $this->dish_model->insert_dish($data);
	}

	function exportLink($to = 'World') {
		//total page
		$total = 10;
		// url list page of lamsao.com
		$web = "http://www.lamsao.com/sinh-to-nuoc-trai-cay-p213c225";
		for ($i=1; $i <= $total; $i++) { 
			$url[] = $web . 'pn' . $i . '.html';
		}

		$articles = array();
		foreach ($url as $key => $value) {
			$this->get_link($value, $articles);
		}

		$this->load->helper('excel');
		$xls = new Excel_XML;
		$xls->addArray($articles);
		echo "thành công !";
		$xls->generateXML ( "sua-che-kem" );
	}

	function out($array='')
	{
		foreach ($array as $key => $value) {
			echo $value .'\n';
		}
	}

	function get_link($url, &$articles){
		$html = file_get_html($url);
		foreach($html->find('.list-articles .GridRow') as $article) {
			$arr = array();
		    $arr[] = trim($article->find('a.title-article', 0)->href);
		    $articles[] = $arr;
		}
	}

	function FunctionName($data = null) {
		if ($data) {
            $config['upload_path'] = './images/content/step/';
            $config['allowed_types'] = 'gif|jpg|png';
			$config['encrypt_name'] = 'TRUE';
			
            $this->load->library('upload', $config);
			if ($this->upload->do_upload('image1')) {
                $data['image1'] = $config['upload_path'] . $this->upload->get_file_name();
            }
			
			for($i = 0; $i < 10; $i++) {
				$nameComment = 'step_' . $i;
				$filePhoto = 'filePhoto_' . $i;
				$h_filePhoto = 'h_filePhoto_' . $i;
				
				if (!$data[$nameComment])
				{
					continue;
				}
				$data['steps'][$i]['comment'] = $data[$nameComment];	
				$data['steps'][$i]['dish_id'] = $dish_id;
				$data['steps'][$i]['image'] = $data[$h_filePhoto];
				
				if ($this->upload->do_upload($filePhoto)) {
					$filename = $config['upload_path'] . $this->upload->get_file_name();					
					$imgData = $this->upload->data();
					parent::watermark($imgData['full_path']);
	                $data['steps'][$i]['image'] = $filename;
	            }
			}

        	$this->dish_model->insert_dish($data);
 		}   
	}

	function csv($file, $list='')
	{
		$file = fopen($file, "w+");

		foreach ($list as $line)
		{
			fputcsv($file,explode(',',$line));
		}

		fclose($file);
	}

	function ngoisaonet_getlink() {
		$web = $_GET['web'];
		if(isset($web)) {
			$articles = array();
			$html = file_get_html($web);
			foreach($html->find('.ulHollywood li.liHollywood') as $article) {
			    echo trim($article->find('a', 0)->href) . "<br/>";
			}
		}
		
		echo "thành công !";
	}

	function ngoisaonet_getdata() {
		$web = $_GET['web'];
		if(isset($web)) {
			$articles = array();
			$html = file_get_html($web);

			$data['name'] = $this->decode($html->find('h1.title', 0)->plaintext);
			$data['introtext'] = $this->decode($html->find('h2.lead', 0)->plaintext);
	    	$data['image'] = $this->decode($html->find('div.fck_detail img', 0)->src);
	    	//$data['stockstring'] = $this->decode($html->find('div.fck_detail p.Normal', 0)->outertext);

			foreach($html->find('div.fck_detail p.Normal') as $article) {
			    $data['stockstring'][] = $this->decode($article->plaintext);
			}

			$i = 1;
			foreach($html->find('table.tbl_insert tr') as $article) {
			    $data['steps'][$i]['image'] = $this->decode($article->find('img', 0)->src);
			    $data['steps'][$i]['comment'] = $this->decode($article->plaintext);
			    $i++;
			}
			var_dump($data);
		}
		
		echo "thành công !";
	}
}
?>
